package com.siloam.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.siloam.module.Prayer;

public class PrayerDao {
	private SqlSession sqlSession;

	public SqlSession getSqlSession() {
		return sqlSession;
	}

	public void setSqlSession(SqlSession sqlSession) {
		this.sqlSession = sqlSession;
	}

	private String getMapper(String method) {
		return "com.siloam.sqlmap.PrayerMapper." + method;
	}
	
	public void add(Prayer prayer){
		sqlSession.insert(getMapper("add"), prayer);
	}
	
	public int findCountByUser(int userId){
		return sqlSession.selectOne(getMapper("findCountByUser"), userId);
	}
	
	public List<Prayer> findPageByUser(int userId, int rowBeginIndex,int rowCount){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("rowBeginIndex", rowBeginIndex);
		params.put("rowCount", rowCount);
		return sqlSession.selectList(getMapper("findPageByUser"), params);
	}
	
	public void delete(int id){
		sqlSession.delete(getMapper("deletePrayer"), id);
	}
	
	public void setStatus(int id, int status){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		param.put("status", status);
		sqlSession.update(getMapper("setStatus"), param);
	}
	
}
